import { setMealDelete, setMealEnabled, setMealList, setMealListParams } from '@/apis/operation/setMealPrice'
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { ElMessage, ElMessageBox } from 'element-plus'
import { reactive } from 'vue'
import { packageNameMap } from '@/utils/config'
import * as tableConfig from './config.json'

export class PriceData extends BaseData implements BaseMethod {
    //套餐弹窗数据
    setMealDialog: any = reactive({
        visible: false,
        type: 'create',
        priceType: 3, //3 单笔 | 套餐 2次数包 1充值赠送
        data: null
    })
    //产品类型 默认 全部 0
    productType = '0'

    constructor() {
        super(tableConfig)
        this.getData()
    }

    onOperation = (arg?: any) => {
        const { type, data } = arg

        if (type === 'edit') {
            //套餐
            this.setMealDialog.visible = true
            this.setMealDialog.type = type
            this.setMealDialog.data = data
        } else if (type === 'create') {
            //套餐
            this.setMealDialog.visible = true
            this.setMealDialog.type = type
        } else if (type === 'delete') {
            ElMessageBox.confirm('确定要删除该价格吗？', '删除', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            })
                .then(() => {
                    setMealDelete(data.id).then(res => {
                        if (res.data.value === 200) {
                            ElMessage.success('操作成功！')
                            this.getData()
                        } else {
                            ElMessage.error(res.data.desc)
                        }
                    })
                })
                .catch(() => {
                    //
                })
        } else if (type === 'switch') {
            setMealEnabled(data.id, Number(data.status)).then(res => {
                if (res.data.value === 200 || res.data.value === 31001 || res.data.value === 31002) {
                    this.getData()
                }
            })
        }
    }

    /**
     * 监听点击搜索按钮
     * @param res
     */
    onSearch = (res: any) => {
        this.pageData.pagination.pageNo = 1
        console.log(res)
        this.productType = res.productType
        this.getData()
    }

    /**
     * 监听点击重置按钮
     */
    onReset = () => {
        this.pageData.pagination.pageNo = 1
        this.productType = '0'
        this.getData()
    }

    /**
     * 监听分页发生改变
     * @param index
     */
    onPaginationChange = (index: number) => {
        this.pageData.pagination.pageNo = index
        this.getData()
    }

    /**
     * 监听选择每页显示个数
     * @param size
     */
    onPaginationSize = (size: number) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = size
        this.getData()
    }

    /**
     * 获取表格数据方法
     * @param status 状态（0.禁用；1.启用）
     */
    getData = () => {
        const params: setMealListParams = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }
        if (Number(this.productType)) {
            params.productType = Number(this.productType)
        }
        setMealList(params).then(res => {
            if (res.data.value === 200) {
                this.pageData.table.data.length = 0
                const arr: any[] = JSON.parse(JSON.stringify(res.data.data.records))
                arr.forEach(item => {
                    item.status = item.status == 1 ? true : false
                    item.discount = (Number(item.marketAmount) - Number(item.amount)).toFixed(2)
                    item.price = Math.fround(Number(item.amount) / Number(item.packageCount)).toFixed(2)
                    item.salesPrice = Number(item.amount).toFixed(2)
                    item.packageName = packageNameMap.get(item.packageNameNumber + '')
                })
                this.pageData.table.data.push(...arr)
                this.pageData.pagination.total = res.data.data.total
            }
        })
    }

    /**
     * 监听用户某个switch 开关
     * @param id
     * @param status
     */
    onStatusChange = (row: any) => {
        console.log(row)
        setMealEnabled(row.id, Number(row.status)).then(res => {
            if (res.data.value === 200 || res.data.value === 31001 || res.data.value === 31002) {
                this.getData()
            }
        })
    }

    /**
     * 监听编辑弹窗隐藏
     */
    onDialogClose = () => {
        //套餐
        this.setMealDialog.visible = false
        this.getData()
    }
}
